package cate.game.role.res.guild.boss;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.common.table.guild.guildboss.GuildBossCombatRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.res.guild.GuildFuncBase;
import cate.game.role.res.guild.boss.msg.GuildBossBuffBuyResp;
import cate.game.role.res.guild.po.GuildPO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@NoteClass(value = "公会boss数据")
public class GuildBossPO extends GuildFuncBase {
    @NoteField(value = "排行榜")
    public GuildBossRankPO rank;

    @NoteField(value = "已通过第几关")
    public int combatNumPass;

    @NoteField(value = "当前boss剩余血量")
    public Long curHp;

    @NoteField(value = "buff层数")
    public int buffNum;

    @NoteField(value = "buff失效时间")
    public long buffInvalidTime;

    @Override
    public void initialize(GuildPO parent) {
        super.initialize(parent);

        lastCombatCheck();
        if (combatNumPass < 0) {
            combatNumPass = 0;
        }
        if (curHp == null || curHp <= 0) {
            GuildBossCombatRow combatTpl = getFuncDependent().guild.game.table.guildBoss.combat.get(combatNumPass + 1);
            if (combatTpl != null) {
                curHp = combatTpl.bossHp;
            } else {
                log.error("公会BOSS第{}关配置缺失", combatNumPass + 1);
                curHp = 0L;
            }
        }
        if (rank == null) {
            rank = new GuildBossRankPO();
        }
        rank.initialize(getFuncDependent().guild);
    }

    /**
     * 被挑战前准备数据（检查数据有效性和修正）
     */
    public synchronized GameResult<Void> prepare(Role role) {
        GameResult<Void> r = new GameResult<>();

        boolean killed = isKilled();
        if (killed) {
            // 防止多人同时挑战最后一关导致出错
            GuildBossCombatRow passRow = getFuncDependent().guild.game.table.guildBoss.combat.get(combatNumPass + 1);
            if (passRow != null) {
                combatNumPass++;
            }
            //
            rank.handleReward();

            lastCombatCheck();
            //
            GuildBossCombatRow combatTpl = getFuncDependent().guild.game.table.guildBoss.combat.get(combatNumPass + 1);
            if (combatTpl != null) {
                curHp = combatTpl.bossHp;
            } else {
                role.getGame().notice.message(role, 700376, "已挑战完所有章节");
                return r.fail("已挑战完所有章节");
            }
        }
        long now = System.currentTimeMillis();
        if (now >= buffInvalidTime) {
            buffNum = 0;
        }
        return r.success();
    }

    /**
     * 如果是已经打完最后一关那就退回到前面一关
     */
    private void lastCombatCheck() {
        if (isLastCombat()) {
            combatNumPass--;
        }
    }

    @JsonIgnore
    public boolean isLastCombat() {
        return getFuncDependent().guild.game.table.guildBoss.combat.get(combatNumPass + 1) == null;
    }

    /**
     * 扣血
     */
    public synchronized void hurt(long damage) {
        curHp -= damage;
    }

    /**
     * 是否被击杀
     */
    @JsonIgnore
    public boolean isKilled() {
        return curHp <= 0;
    }

    /**
     * buff层数购买+1
     */
    public void buffBought() {
        if (buffNum >= GDGuild.boss.BUFF_NUM_MAX) {
            return;
        }
        buffNum++;

        long now = System.currentTimeMillis();
        if (buffInvalidTime < now) {
            buffInvalidTime = now;
        }
        buffInvalidTime += 2 * 3600 * 1000;
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.BOSS;
    }

    public void buyBuff(Role role) {
        if (!this.guildFuncOpenCheck(role)) {
            return;
        }
        if (getFuncDependent().guild != null) {
            // buff层数检测
            long now = System.currentTimeMillis();
            if (now >= getFuncDependent().guild.boss.buffInvalidTime) {
                getFuncDependent().guild.boss.buffNum = 0;
            }

//            if (guild.boss.buffNum >= GDGuild.boss.BUFF_NUM_MAX) {
//                game.notice.message(role, 700373,"已达到BUFF最大购买次数");
//                return;
//            }
            MixRes cost = new MixRes(GDGuild.boss.BUFF_COST);
            if (!cost.consumeCheck(role, null).ok()) {
                role.getGame().notice.message(
                        role, 310457, "费用不足");
                return;
            }
//            role.cur.op.setOperation(GDOperation.GUILD_BOSS_BUY_BUFF);
            cost.consume(role);
//            role.cur.op.clearOperation();
            getFuncDependent().guild.boss.buffBought();
            // 记录公会活跃
            role.getRes().guild.active.addActive(GDGuild.activeTask.ADD_GUILD_BUFF);
            role.getHistory().action.guildBossBuff();
            role.getGame().notice.message(role, "鼓舞成功");
            // 记录公会日志
//            guild.log.addLog(game,new GuildLogDetailPO(GDGuild.log.ADD_GUILD_BUFF, role.getBase().name));
            role.sendNow(new GuildBossBuffBuyResp(getFuncDependent().guild.boss.buffNum, funcDependent.guild.boss.buffInvalidTime));
        }
    }

    /**
     * 获取当前挑战BOSS序号
     */
    @JsonIgnore
    public int getCurrentCombatNum() {
        if (isLastCombat()) {
            return combatNumPass;
        }
        return combatNumPass + 1;
    }
}
